//
//  MDDLoginViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/17.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDLoginViewController.h"
#import "PPTextfield.h"
#import "AppDelegate.h"
#import "MDDTabBarController.h"
#import "MDDPerfectInfoViewController.h"
#import "UserAgreementViewController.h"

@interface MDDLoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet PPTextfield *phoneTF;

@property (weak, nonatomic) IBOutlet PPTextfield *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *selBtn;
@end
// 账号  15074187501  
@implementation MDDLoginViewController
- (IBAction)userDelegateAction:(id)sender {
    UserAgreementViewController * uavc = mGetStoryboardViewController(@"Main", @"userAgreementViewController");
    uavc.fileName = @"Platform_agreement.html";
    uavc.title = @"用户协议";
    [self.navigationController pushViewController:uavc animated:YES];
}
- (IBAction)yisiAction:(id)sender {
    UserAgreementViewController * uavc = mGetStoryboardViewController(@"Main", @"userAgreementViewController");
    uavc.fileName = @"Privacy_agreement.html";
    uavc.title = @"隐私协议";
    [self.navigationController pushViewController:uavc animated:YES];
}
- (IBAction)selAction:(id)sender {
    self.selBtn.selected = !self.selBtn.selected;
}
- (IBAction)getCodeAction:(id)sender {
    if (![self.phoneTF.text isMobile]) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入正确的手机号码"];
        return;
    }
    if (!self.selBtn.selected) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请阅读并同意用户协议跟隐私政策"];
        return;
    }
    self.getCodeBtn.userInteractionEnabled = NO; // 关闭触发
    [self settingRunBtn:self.getCodeBtn];       // 设置倒计时中的颜色
    // 函数触发模式，无先后顺序，设置即可触发  ************************************************
    // 开始发起倒计时
    [self.getCodeBtn AxcUI_countDownWithTimeInterval:60 countDownFormat:@"%zd秒后重试"];
    __weak typeof(self) weakSelf = self;
    // 倒计时结束后的Block回调
    self.getCodeBtn.axcUI_buttonTimeStoppedCallback = ^{
        weakSelf.getCodeBtn.userInteractionEnabled = YES;       // 设置启用触发
        
        [weakSelf settingBtn:weakSelf.getCodeBtn];              // 设置初始的颜色等
        NSLog(@"倒计时完毕");
    };
    NSMutableDictionary * info = [[NSMutableDictionary alloc]initWithDictionary:@{@"phone":self.phoneTF.text,@"add":@"1"}];
    [PPNetworkHelper POST:GETPHONECODE parameters:info success:^(id responseObject) {
        
    } failure:^(NSError *error) {
        
    } showLoading:self.view];
}
-(void)rightAction:(UIButton *)sender
{
    NSDictionary * info;
    info = @{@"mobile":@"13975856648",@"identifyingCode":@"8888"};
    [PPNetworkHelper POST:user_phoneloginwithsms parameters:info success:^(id responseObject) {
        
        if (YJPPSUCCESS(responseObject)) {
            AppUserBaseModel * appModel = [[AppUserBaseModel alloc]init];
            [appModel setValuesForKeysWithDictionary:responseObject[@"data"]];
            [AppUserBaseModel storeUserInfoWithModel:appModel];
            [self getUserInfo];
            if (self.isPush) {
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                AppDelegate * app = mAppDelegate;
                MDDTabBarController * mainRun = mGetStoryboardViewController(@"Main", @"mainRun");
                app.window.rootViewController = mainRun;
            }
        }else{
            if (YJPPFAILCODE(responseObject) == 500) {
                if ([responseObject[@"data"][@"code"] integerValue] == -3) {
                    MDDPerfectInfoViewController * pivc = mGetStoryboardViewController(@"Main", @"mDDPerfectInfoViewController");
                    pivc.phoneT = self.phoneTF.text;
                    NavigationController_pushVC(pivc);
                }else if([responseObject[@"data"][@"code"] integerValue] == -5){
                    [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"msg"]];
                }else if([responseObject[@"data"][@"code"] integerValue] == -4){
                    MDDPerfectInfoViewController * pivc = mGetStoryboardViewController(@"Main", @"mDDPerfectInfoViewController");
                    pivc.phoneT = self.phoneTF.text;
                    AppUserBaseModel * shareUser = [[AppUserBaseModel alloc]init];
                    [shareUser setValuesForKeysWithDictionary:responseObject[@"data"][@"user"]];
                    pivc.userInfo = shareUser;
                    NavigationController_pushVC(pivc);
                    [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"msg"]];
                }
            }
        }
    } failure:^(NSError *error) {
        
    } showLoading:nil];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.phoneTF.isOnlyNumber = YES;
    self.phoneTF.maxTextLength = 11;
    
    self.codeTF.isOnlyNumber = YES;
    self.codeTF.maxTextLength = 6;
    mNavitationTitle(@"登录");
    self.hbd_swipeBackEnabled = NO;
    self.navigationItem.hidesBackButton = YES;
    
    [self shenheIng];
}
-(void)shenheIng{
    
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, @"1", @"add");
    [PPNetworkHelper POST:app_isOpenVisitorModel parameters:parameters success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            if ([responseObject[@"data"][@"isOpenVisitorModel"] isEqualToString:@"1"]) {
                [self addRightBarButtonItemWithTitle:@"游客登录" action:@selector(rightAction:)];
            }else{
                
            }
            
        }else{
            
        }
    } failure:^(NSError *error) {
        
    } showLoading:nil];
    
}
- (IBAction)sureLoginAction:(id)sender {
    
    if (!self.selBtn.selected) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请阅读并勾选用户使用协议"];
        return;
    }
    if (![self.phoneTF.text isMobile]) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入正确的手机号码"];
        return;
    }
    if (self.codeTF.text.length == 0) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入验证码"];
        return;
    }
    NSDictionary * info;
    info = @{@"mobile":self.phoneTF.text,@"identifyingCode":self.codeTF.text};
    [PPNetworkHelper POST:user_phoneloginwithsms parameters:info success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            AppUserBaseModel * appModel = [[AppUserBaseModel alloc]init];
            [appModel setValuesForKeysWithDictionary:responseObject[@"data"]];
            [AppUserBaseModel storeUserInfoWithModel:appModel];
            [self getUserInfo];
            if (self.isPush) {
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                AppDelegate * app = mAppDelegate;
                MDDTabBarController * mainRun = mGetStoryboardViewController(@"Main", @"mainRun");
                app.window.rootViewController = mainRun;
            }
        }else{
            if (YJPPFAILCODE(responseObject) == 500) {
                if ([responseObject[@"data"][@"code"] integerValue] == -3) {
                    MDDPerfectInfoViewController * pivc = mGetStoryboardViewController(@"Main", @"mDDPerfectInfoViewController");
                    pivc.phoneT = self.phoneTF.text;
                    NavigationController_pushVC(pivc);
                }else if([responseObject[@"data"][@"code"] integerValue] == -5){
                    [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"msg"]];
                }else if([responseObject[@"data"][@"code"] integerValue] == -4){
                    MDDPerfectInfoViewController * pivc = mGetStoryboardViewController(@"Main", @"mDDPerfectInfoViewController");
                    pivc.phoneT = self.phoneTF.text;
                    AppUserBaseModel * shareUser = [[AppUserBaseModel alloc]init];
                    [shareUser setValuesForKeysWithDictionary:responseObject[@"data"][@"user"]];
                    pivc.userInfo = shareUser;
                    NavigationController_pushVC(pivc);
                    [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"msg"]];
                }
            }
        }
    } failure:^(NSError *error) {
        
    } showLoading:self.view];
}

-(void)getUserInfo
{
    NSMutableDictionary * info = [[NSMutableDictionary alloc]init];
    [PPNetworkHelper POST:app_getUserInfo parameters:info success:^(id responseObject) {
        
    } failure:^(NSError *error) {
        
    } showLoading:self.view];
}
- (void)settingRunBtn:(UIButton *)sender{
     [sender setTitleColor:[UIColor AxcUI_colorWithHexCode:@"#b4b4b4"] forState:UIControlStateNormal];
    //    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //    sender.layer.borderColor = [[UIColor lightGrayColor] CGColor];
}

- (void)settingBtn:(UIButton *)sender{
    [sender setTitleColor:[UIColor AxcUI_colorWithHexCode:@"#333333"] forState:UIControlStateNormal];
    [sender setTitle:@"重新获取" forState:UIControlStateNormal];
}

@end
